package vmcs.customer.state;

import vmcs.common.VMCSException;
import vmcs.controller.TransactionController;

public class DrinkDispensedState implements State {

	private TransactionController transactionController;
	private static DrinkDispensedState instance;

	private DrinkDispensedState(TransactionController transactionController) {
		this.transactionController = transactionController; 
	}

	public static DrinkDispensedState getInstance(TransactionController transactionController) {
		if (instance == null) 
			instance = new DrinkDispensedState(transactionController);
		return instance;
	}

	@Override
	public void selectDrink(int drinkSelected) {
		// TODO Auto-generated method stub

	}

	@Override
	public void insertCoin(int value) {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispenseDrink(int drinkSelected) {
		// TODO Auto-generated method stub

	}

	@Override
	public void giveChange(int changeRequired) {
		if(changeRequired > 0){
			boolean changeGiven = false;
			try {
				changeGiven = transactionController.getChangeGiver().giveChange(changeRequired);
			} catch (VMCSException e) {
				if(!changeGiven){
					transactionController.terminateMachine();
					return;
				}
			}



		}

		transactionController.setState(transactionController.getChangeGivenState());
		transactionController.storeCash();

	}



	@Override
	public void cancel() {
		// TODO Auto-generated method stub

	}

	@Override
	public void storeCash() {
		// TODO Auto-generated method stub

	} 

	@Override
	public void terminateMachine() {

		transactionController.getDispenseController().allowSelection(false);

		transactionController.getCoinReceiver().stopReceive();

		transactionController.getCoinReceiver().refundCash();  

		transactionController.setState(transactionController.getMachineTerminatedState());

	} 


}
